package com.example.demo.controller.system;

import com.example.demo.annoation.Log;
import com.example.demo.bean.AjaxRes;
import com.example.demo.bean.sysuser.SysUserDO;
import com.example.demo.controller.BaseAdminController;
import com.example.demo.enums.BusinessType;
import com.example.demo.service.ISysUserService;
import com.example.demo.dto.sysuser.SysUserPageReqVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping(value = "/system/sys_user")
@RestController
public class SysUserController extends BaseAdminController {

    @Resource
    private ISysUserService sysUserService;

    @GetMapping("/list")
    public AjaxRes list(SysUserPageReqVO sysUserPageReqVO) {
        return AjaxRes.success(sysUserService.list(sysUserPageReqVO));
    }

    @PostMapping("/insert")
    @Log(title = "新增用户", businessType = BusinessType.INSERT)
    public AjaxRes insert(@RequestBody SysUserDO sysUserDO) {
        return toAjax(sysUserService.addSysUser(sysUserDO));
    }

    @DeleteMapping("/delete/{userId}")
    @Log(title = "删除用户", businessType = BusinessType.DELETE)
    public AjaxRes delete(@PathVariable("userId") Long userId) {
        return toAjax(sysUserService.deleteSysUser(userId));
    }

    @PutMapping("/update")
    @Log(title = "修改用户信息", businessType = BusinessType.UPDATE)
    public AjaxRes update(@RequestBody SysUserDO sysUserDO) {
        return toAjax(sysUserService.updateSysUser(sysUserDO));
    }

    @PutMapping("/changeStatus")
    @Log(title = "修改用户状态", businessType = BusinessType.UPDATE)
    public AjaxRes changeStatus(@RequestBody SysUserDO sysUserDO) {
        return toAjax(sysUserService.changeSysUserStatus(sysUserDO));
    }

    @PutMapping("/resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    public AjaxRes resetPwd(@RequestBody SysUserDO sysUserDO){
        return toAjax(sysUserService.resetPwd(sysUserDO));
    }

    @GetMapping("/{userId}")
    public AjaxRes query(@PathVariable("userId") Long userId) {
        return AjaxRes.success(sysUserService.getSysUser(new SysUserDO().setUserId(userId)));
    }
}
